# long 爬虫
# {2021/9/9}
# 本章目的：
# 从网站拿到源码
# 从源码找到m3u8的url
# 下载m3u8
# 读取m3u8，下载视频
# 合并视频
import requests

# # url = "https://dd520.cc/dianying/74551-5-1.html"
# header = {
#     "cookie": "UM_distinctid=17bc64f54c64c8-0351234ee31f99-3e604809-1fa400-17bc64f54c7612; CNZZDATA1280148776=892512959-1631110876-https%253A%252F%252Fwww.555dy2.com%252F%7C1631110876; ecPopup=1; _ga=GA1.1.18814675.1631119693; PHPSESSID=q1agvhvam91kobkqrt9hac1jm3; mac_history=%7Blog%3A%5B%7B%22name%22%3A%22%5B%E5%89%A7%E6%83%85%E7%89%87%5D%E4%BA%86%E4%B8%8D%E8%B5%B7%E7%9A%84%E8%80%81%E7%88%B8%22%2C%22link%22%3A%22https%3A%2F%2Fdd520.cc%2Fdianying%2F74551-2-1.html%22%2C%22pic%22%3A%22https%3A%2F%2Fimg10.360buyimg.com%2Fddimg%2Fjfs%2Ft1%2F179903%2F7%2F19877%2F225690%2F612109c5E7f88ad8f%2Fe13f43d1d9aa3ec1.jpg%22%2C%22mid%22%3A%22%22%7D%2C%7B%22name%22%3A%22%5B%E5%9B%BD%E4%BA%A7%E5%89%A7%5D%E4%BA%91%E5%8D%97%E8%99%AB%E8%B0%B7%22%2C%22link%22%3A%22https%3A%2F%2Fdd520.cc%2Fdianying%2F83601-1-1.html%22%2C%22pic%22%3A%22http%3A%2F%2Fpuui.qpic.cn%2Fvcover_vt_pic%2F0%2Fmzc00200wk4wxlp1630291228463%2F0%22%2C%22mid%22%3A%22-%E7%AC%AC1%E9%9B%86%22%7D%5D%7D; _ga_5834174Y43=GS1.1.1631119693.1.1.1631120297.0",
#     "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
# }
# # data = requests.get(url,headers=header)
#
# m3u8_url = "https://yuncache.37581.com.cn:6688/jhyun.php?key=86fd0c346e44388adc195f5eb05215e4923c00ff3a0d167053c5ea037db25ea5f0b911259f77c27e6645f60e5a060b4fed12a5693c7945169f17581ad755160cb35d8a16cee15267857a7e50db36566968cf373c5019d528eb838c476875fae86e5a623f845d065db02479a7&sign=5d12c3ecda101475529c4634daf8ed4c&t=1631121066&name=jhyun.m3u8"
# data = {
#     "key": "86fd0c346e44388adc195f5eb05215e4923c00ff3a0d167053c5ea037db25ea5f0b911259f77c27e6645f60e5a060b4fed12a5693c7945169f17581ad755160cb35d8a16cee15267857a7e50db36566968cf373c5019d528eb838c476875fae86e5a623f845d065db02479a7",
#     "sign": "5d12c3ecda101475529c4634daf8ed4c",
#     "t": 1631121066,
#     "name": "jhyun.m3u8"
# }
# resp2 = requests.get(m3u8_url,headers=header,data=data)
# with open("了不起的老爸：在线播放.m3u8",mode="wb") as f:
#     f.write(resp2.content)
#
# resp2.close()
# print("下载完成")

# 解析m3u8
n=1
with open("了不起的老爸：在线播放.m3u8",mode="r",encoding='utf-8') as f:
   for line in f:
        line = line.strip() #先去掉空格空行
        if line.startswith("#"): #如果以#开头，我不要
            continue
        # 下载视频片段

        resp3 = requests.get(line)
        f = open(f"video/{n}.ts",mode="wb")
        f.write(resp3.content)
        f.close()
        resp3.close()
        n += 1
        print("完成1个")

